﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using EDXLCoT;
using EDXLSharp;
using EDXLSharp.CAPLib;
using EDXLSharp.EDXLDELib;
using EDXLSharp.EDXLHAVE_2_0Lib;
using EDXLSharp.EDXLRMLib;
using ICNETServices;
using MEXLSitRepLib;
using MEXLTEPLib;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.Model;
using Amazon.SQS;
using Amazon.Util;
using System.Text;

namespace ICNETDAL
{
  public static class AWSUtils
  {
    private static AmazonSQSConfig amazonSQSConfig;

    /// <summary>
    /// Amazon SQS Client
    /// </summary>
    private static AmazonSQSClient amazonSQSClient;

    /// <summary>
    /// Amazon DynamoDB configuration object
    /// </summary>
    private static AmazonDynamoDBConfig amazonDynamoConfig;

    /// <summary>
    /// Amazon DynamoDB client
    /// </summary>
    private static AmazonDynamoDBClient amazonDynamoClient;

    static AWSUtils()
    {
      amazonSQSConfig = new AmazonSQSConfig();
      amazonSQSConfig.ServiceURL = AWSConstants.SQSServiceURL;
      amazonSQSClient = new AmazonSQSClient(amazonSQSConfig);
      amazonDynamoConfig = new AmazonDynamoDBConfig();
      amazonDynamoConfig.ServiceURL = AWSConstants.DynamoServiceURL;
      amazonDynamoClient = new AmazonDynamoDBClient(amazonDynamoConfig);
    }

    /// <summary>
    /// Global instance of a dynamo db client
    /// </summary>
    /// <returns>Dynamo DB Client</returns>
    public static AmazonDynamoDBClient DynamoClient
    {
      get
      {
        return amazonDynamoClient;
      }
    }

    /// <summary>
    /// Create a Log Message from a set of Dynamo Attributes
    /// </summary>
    /// <param name="AttVals">Attributes</param>
    /// <returns></returns>
    public static string CreateLogMessage(Dictionary<string, AttributeValue> AttVals)
    {
      StringBuilder sbMessage = new StringBuilder();

      foreach (KeyValuePair<string, AttributeValue> kvp in AttVals)
      {
        string attributeName = kvp.Key;
        AttributeValue value = kvp.Value;

        StringBuilder item = new StringBuilder();
        item.Append( "(" +
          attributeName + " " +
          (value.S == null ? "" : "S=[" + value.S + "]") +
          (value.N == null ? "" : "N=[" + value.N + "]") +
          (value.SS == null ? "" : "SS=[" + string.Join(",", value.SS.ToArray()) + "]") +
          (value.NS == null ? "" : "NS=[" + string.Join(",", value.NS.ToArray()) + "]") +
          ")"
        );
        sbMessage.Append(item.ToString());
      }
      return sbMessage.ToString();
    }
  }
}
